﻿
<div id="MonitorCenterMap" class="MonitorFlexBox-V MonitorFlex-1">
    <div id="MonitorMapMenuDiv" class="MenuDiv">
        <input type="button" value="清除所有绘图" onclick="ClearOverlays()" />
        <input type="button" value="清除最近绘图" onclick="ClearLastOverlay()" />
    </div>
    <div id="ResizableDiv">
        <div id="MapDiv"></div>
    </div>
@*    <div id="MonitorDataMenuDiv" class="MenuDiv">
        <input type="button" value="显示设置"/>
    </div>*@
        <div id="GpsDiv" class=" MonitorFlex-1">
            <table id="GpsTable" class="easyui-datagrid"
                rownumbers="true"
                singleSelect="true"
                striped="true"
                loadMsg=""
                fit="true"
                height="100"
                remoteSort="false"
                data-options="onDblClickRow:OnDblClickRow">
                <thead>
                    <tr>
                        <th field="name" width="100">车牌号</th>
                        <th field="sim" width="100">SIM卡号</th>
                        <th field="depart" width="100">所属单位</th>
                        <th field="lng" width="100">经度</th>
                        <th field="lat" width="100">纬度</th>
                        <th field="addr" width="320">地址</th>
                        <th field="gpsSpeed" width="80" sortable="true">GPS速度</th>
                        <th field="gpsMileage" width="80" sortable="true">GPS里程</th>
                        <th field="devSpeed" width="80" sortable="true">仪表速度</th>
                        <th field="devMileage" width="80" sortable="true">仪表里程</th>
                        <th field="oil" width="50" sortable="true">油量</th>
                        <th field="state41" width="50">正反转</th>
                        <th field="state0" width="50">ACC</th>
                        <th field="dire" width="50">方向</th>
                        <th field="time" width="130" sortable="true">上传时间</th>
                    </tr>
                </thead>
            </table>
        </div>
</div>
<script type="text/javascript">
    $(document).ready(function () {
        //隐藏或者显示
        //$("#MapDivisionDiv").click(function () {
        //    $("#GpsWrap").slideToggle(500);
        //    if ($("#MapDivisionDiv").hasClass("DivisionMarginBottom")) {
        //        $("#MapDivisionDiv").removeClass("DivisionMarginBottom");
        //    }
        //    else {
        //        $("#MapDivisionDiv").addClass("DivisionMarginBottom");
        //    }
        //});
        //alert($("#MonitorCenterMap").height() - $("#MonitorMapMenuDiv").height() - $("#MonitorDataMenuDiv").height());

        SetResizable();
        //窗口放大缩小时
        $(window).resize(function () {
            SetResizable();
        });

        GetRegions();
    });
    //设置resizable
    function SetResizable() {
        var height = $("#MonitorCenterMap").height() - $("#MonitorMapMenuDiv").height() - $("#MonitorDataMenuDiv").height() - 20;
        $("#ResizableDiv").height(height * 2 / 3);
        $("#ResizableDiv").resizable({
            minHeight: 0,
            maxHeight: height,
            handles: "s",
            onStopResize: OnResize
        });
        $("#GpsTable").datagrid("resize", {
            width: function () { return $("#GpsDiv").width(); },
            height: function () { return $("#GpsDiv").height(); }
        })
    }
    //OnResize
    function OnResize() {
        $("#GpsTable").datagrid("resize", {
            width: function () { return $("#GpsDiv").width(); },
            height: function () { return $("#GpsDiv").height(); }
        })
    }
    //根据sim卡号获取实时数据
    function GetGpsData(sims) {
        if (sims == null || sims.length == 0) {
            return;
        }
        else {
            var time = "1990-01-01 00:00:00";
            var times = [];
            for (var i = 0; i < sims.length; i++) {
                if (vehiclesGpsInfo[sims[i]].time == null) {//缓冲中没有该sim卡的gps信息
                    times.push(time);
                }
                else {//若有就将上次更新时间发到服务器
                    times.push(vehiclesGpsInfo[sims[i]].time);
                }
            }
            $.ajax({
                type: "post",
                url: "/MonitorCenter/GetGpsData",
                data: {
                    "sims": sims,
                    "times": times
                },
                traditional: true,
                success: function (data) {
                    if (data != null && data.total != 0) {
                        //更新有最新gps数据的车辆
                        for (var i = 0; i < data.rows.length; i++) {
                            var tmp = vehiclesGpsInfo[data.rows[i].sim];
                            tmp.lng = data.rows[i].lng;//经度
                            tmp.lat = data.rows[i].lat;//纬度
                            tmp.addr = data.rows[i].addr;//地址
                            tmp.dire = data.rows[i].dire;//方向
                            tmp.gpsSpeed = data.rows[i].gpsSpeed;//GPS速度
                            tmp.gpsMileage = data.rows[i].gpsMileage;//GPS里程
                            tmp.devSpeed = data.rows[i].devSpeed;//仪表速度
                            tmp.devMileage = data.rows[i].devMileage;//仪表里程
                            tmp.oil = data.rows[i].oil;//油量
                            tmp.state0 = data.rows[i].state0;//acc
                            tmp.time = data.rows[i].time;//上传时间
                            tmp.state41 = data.rows[i].state41;//正反转
                        }
                        setTimeout(function () {//更新有最新GPS信息的车辆，没有的不更新
                            for (var i = 0; i < data.rows.length; i++) {
                                var tmp = vehiclesGpsInfo[data.rows[i].sim];
                                UpdateVehicleFromDataGrid(tmp);//更新有最新GPS信息的车辆到DataGrid
                            }
                            //AutoSizeColumn();
                        }, 100);
                    }
                    setTimeout(function () {//更新所有选中车辆状态
                        if (sims.length == times.length) {
                            for (var i = 0; i < sims.length; i++) {
                                UpdateNodeIcon(sims[i], times[i], vehiclesGpsInfo[sims[i]].state0);//更新节点状态；离线，在线熄火，在线启动
                            }
                        }
                    }, 100);
                }
            });
        }
    }
    //重新加载table
    function ReloadTable(addNodes, delNodes) {
        $("#GpsTable").datagrid("loadData", { total: addNodes.length, rows: addNodes });
        for (var i = 0; i < addNodes.length; i++) {
            AddMarker(addNodes[i]);
        }
        for (var i = 0; i < delNodes.length; i++) {
            DelMarker(delNodes[i]);
        }
    }
    //获取用户下的所有区域
    function GetRegions() {
        $.ajax({
            type: "post",
            url: "/MonitorCenter/GetRegions",
            success: function (data) {
                if (data != null && data.count != 0) {
                    //画区域
                    for (var i = 0; i < data.rows.length; i++) {
                        var row = data.rows[i];
                        addRegion(row.mLng, row.mLat, row.mRadius, row.mName);
                    }
                }
            }
        });
    }
    //增加一部车辆信息到DataGrid
    function AddVehicleToDataGrid(sim) {
        var row = vehiclesGpsInfo[sim];
        //if (row != null && row.time != null) {
        //    $("#GpsTable").datagrid("appendRow", vehiclesGpsInfo[sim]);
        //}
        if (row != null) {
            //var index = $("#GpsTable").datagrid("getRowIndex", row);
            //if (index < 0) {//不存在才添加
            $("#GpsTable").datagrid("appendRow", vehiclesGpsInfo[sim]);

            AddMarker(row);
            //}
        }
    }
    //调整DataGrid的列宽
    function AutoSizeColumn() {
        $("#GpsTable").datagrid("autoSizeColumn");//调整列宽
    }
    //从DataGrid删除一部车辆信息
    function DeleteVehicleFromDataGrid(sim) {
        var row = vehiclesGpsInfo[sim];
        //if (row != null && row.time != null) {
        //    var index = $("#GpsTable").datagrid("getRowIndex", row);
        //    if (index != null && index >= 0) {
        //        $("#GpsTable").datagrid("deleteRow", index);
        //    }
        //}
        if (row != null) {
            var index = $("#GpsTable").datagrid("getRowIndex", row);
            if (index != null && index >= 0) {
                $("#GpsTable").datagrid("deleteRow", index);
                DelMarker(row);
            }
        }
    }
    //从DataGrid中更新车辆信息
    function UpdateVehicleFromDataGrid(row) {
        var index = $("#GpsTable").datagrid("getRowIndex", row);
        if (index != null && index >= 0) {
            $("#GpsTable").datagrid("updateRow", {
                index: index,
                row: row
            });
            ModMarker(row);
        }
    }
    //双击实时数据,获取焦点
    function OnDblClickRow(rowIndex, rowData) {
        FocusAndCheckNode(rowData)
    }

    //根据sim卡号选择行并在地图获取焦点
    function SelectRowAndFocus(sim) {
        var row = vehiclesGpsInfo[sim];
        var index = $("#GpsTable").datagrid("getRowIndex", row);
        if (index >= 0) {
            $("#GpsTable").datagrid("selectRow", index);
            $("#GpsTable").datagrid("scrollTo", index);
            FocusMarker(row);
        }
    }
</script>
